home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-07-27 | 2.5 KB | 109 lines |
- /*
- * LevelsManager.java - A levels manager for eCross
- * Copyright (C) 2000 Romain Guy
- * guy.romain@bigfoot.com
- * www.jext.org
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
- import waba.io.*;
-
- /**
- * This class is used to build levels from a database.
- * @author Romain Guy <guy.romain@bigfoot.com>
- * @version 1.0
- */
-
- public class LevelsManager
- {
- // the levels database
- private String dataBase;
- private byte count;
-
- public LevelsManager(String dataBase)
- {
- this.dataBase = dataBase;
- }
-
- public byte getLevelsCount()
- {
- return count;
- }
-
- /**
- * Sets the current level.
- * @param level The level number in the database
- */
-
- public Level getLevel(int level)
- {
- Catalog levels = new Catalog(dataBase, Catalog.READ_ONLY);
- if (!levels.isOpen())
- return null;
- count = (byte) levels.getRecordCount();
-
- byte[] b;
- String name;
-
- if (!levels.setRecordPos(level))
- {
- levels.close();
- return null;
- }
-
- byte[] high = new byte[2];
- if (levels.readBytes(high, 0, 2) != 2)
- {
- levels.close();
- return null;
- }
-
- b = new byte[Level.NAME_SPACE];
- if (levels.readBytes(b, 0, b.length) != b.length)
- {
- levels.close();
- return null;
- }
-
- int count = 0;
- char[] c = new char[Level.NAME_SPACE];
-
- for (int i = 0; i < c.length; i++)
- {
- byte _data = b[i];
- if (_data != -1)
- {
- c[i] = (char) _data;
- count++;
- } else
- break;
- }
- name = new String(c, 0, count);
-
- b = new byte[levels.getRecordSize() - Level.NAME_SPACE - 2];
- if (levels.readBytes(b, 0, b.length) != b.length)
- {
- levels.close();
- return null;
- }
-
- levels.close();
- return new Level(high, name, b);
- }
- }
-
- // End of LevelsManager.java
-